//
//  PYViewController.m
//  IHome
//
//  Created by zhujizheng on 2020/2/15.
//  Copyright © 2019年 com.zhujizheng. All rights reserved.
//

#import "PYViewController.h"

@interface PYViewController ()

@end

@implementation PYViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = PY_APP_Controller_backgroundColor;
    if ([self needBackArrow]) {
        [self addBackArrowWithDefaultAction];
    }
    if ([self autoFitSizeForNaviTitle]) {
        UILabel *lbl = [UILabel new];
        lbl.text = self.navigationItem.title;
        lbl.numberOfLines = 0;
        lbl.textAlignment = NSTextAlignmentCenter;
        self.navigationItem.titleView = lbl;
    }
    [self addNotifications];
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    NSLog(@"进入%@", NSStringFromClass([self class]));
    [self refreshRedPoint];
}

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    if ([self needRefreshViewAfterViewDidAppear]) {
        [UIView animateWithDuration:PY_Animation_Duration animations:^{
            [self.navigationController.view setNeedsLayout];
            [self.navigationController.view layoutIfNeeded];
        }];
    }
}

#pragma mark - 是否需要在viewDidAppear之后刷新
- (BOOL)needRefreshViewAfterViewDidAppear
{
    return NO;
}

#pragma mark - 是否需要返回箭头
- (BOOL)needBackArrow
{
    return YES;
}

#pragma mark - 导航栏标题自动换行
- (BOOL)autoFitSizeForNaviTitle
{
    return NO;
}

#pragma mark - 刷新红点
/// 刷新红点
- (void)refreshRedPoint
{
    // 子类重写
}

#pragma mark - 顶部白色遮罩
- (UIView *)topWhiteView
{
    if (!_topWhiteView) {
        _topWhiteView = [UIView new];
        _topWhiteView.backgroundColor = [UIColor whiteColor];
    }
    return _topWhiteView;
}
/// 调整topWhiteView的高度
- (void)adjustTopWhiteViewHeight:(CGFloat)height
{
    [self.view addSubview:self.topWhiteView];
    self.topWhiteView.frame = CGRectMake(0, 0, APP_WIDTH, MAX(0, height + 1));
}

#pragma mark - 导航栏按钮
/// 设置导航栏返回按钮为箭头图标
- (UIBarButtonItem *)addBackArrowWithDefaultAction
{
    UIBarButtonItem *barButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"back_white_icon"] style:UIBarButtonItemStylePlain target:self action:@selector(backAction)];
    NSMutableArray *items = [NSMutableArray arrayWithArray:self.navigationItem.leftBarButtonItems];
    [items addObject:barButtonItem];
    self.navigationItem.leftBarButtonItems = items;
    return barButtonItem;
}
/// 默认返回事件
- (void)backAction
{
    [self.navigationController popViewControllerAnimated:YES];
}
/// 设置导航栏左边的按钮
- (UIBarButtonItem *)addLeftBarButtonItemWithTitle:(NSString *)title action:(nullable SEL)action
{
    UIBarButtonItem *barButtonItem = [[UIBarButtonItem alloc] initWithTitle:title style:UIBarButtonItemStylePlain target:self action:action];
    NSMutableArray *items = [NSMutableArray arrayWithArray:self.navigationItem.leftBarButtonItems];
    [items addObject:barButtonItem];
    self.navigationItem.leftBarButtonItems = items;
    return barButtonItem;
}
/// 设置导航栏右边的按钮
- (UIBarButtonItem *)addRightBarButtonItem:(UIView *)customView
{
    UIBarButtonItem *barButtonItem = [[UIBarButtonItem alloc] initWithCustomView:customView];
    NSMutableArray *items = [NSMutableArray arrayWithArray:self.navigationItem.rightBarButtonItems];
    [items addObject:barButtonItem];
    self.navigationItem.rightBarButtonItems = items;
    return barButtonItem;
}
/// 设置导航栏右边按钮，带图片，action
- (UIBarButtonItem *)addRightBarButtonItemWithImage:(UIImage *)img action:(nullable SEL)action
{
    UIBarButtonItem *barButtonItem = [[UIBarButtonItem alloc] initWithImage:img style:UIBarButtonItemStylePlain target:self action:action];
    NSMutableArray *items = [NSMutableArray arrayWithArray:self.navigationItem.rightBarButtonItems];
    [items addObject:barButtonItem];
    self.navigationItem.rightBarButtonItems = items;
    return barButtonItem;
}
/// 设置导航栏右边按钮，带文字，action
- (UIBarButtonItem *)addRightBarButtonItemWithTitle:(NSString *)title action:(nullable SEL)action
{
    UIBarButtonItem *barButtonItem = [[UIBarButtonItem alloc] initWithTitle:title style:UIBarButtonItemStylePlain target:self action:action];
    NSMutableArray *items = [NSMutableArray arrayWithArray:self.navigationItem.rightBarButtonItems];
    [items addObject:barButtonItem];
    self.navigationItem.rightBarButtonItems = items;
    return barButtonItem;
}

#pragma mark - 添加通知
/// 添加通知
- (void)addNotifications
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

- (void)statusBarFrameChangedNotification:(NSNotification *)notify
{
    [self statusBarFrameChanged];
}

- (void)statusBarFrameChanged
{

}
#pragma mark - 设置侧滑手势是否可用
/// 设置侧滑手势是否可用
- (void)setBackEdgeGestureEnabled:(BOOL)enabled
{
    if ([self.navigationController isKindOfClass:[PYNavigationController class]]) {
        PYNavigationController *navi = (PYNavigationController *)self.navigationController;
        [navi setBackEdgeGestureEnabled:enabled];
    }
}
#pragma mark - 气泡提示
- (void)showAutoHUD:(NSString *)text
{
    if (!text) {
        NSLog(@"气泡提示的信息为空");
        return;
    }
    [MBProgressHUD py_showText:text forView:self.view];
}
- (void)showIndicatorHUD:(NSString *)text
{
    [MBProgressHUD py_showIndicatorWithText:text forView:self.view];
}
- (void)hideHUD
{
    [MBProgressHUD py_hideHUDForView:self.view];
}
#pragma mark - 屏幕旋转
- (void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator
{
    [super viewWillTransitionToSize:size withTransitionCoordinator:coordinator];
    [coordinator animateAlongsideTransition:^(id<UIViewControllerTransitionCoordinatorContext>  _Nonnull context) {
        self.topWhiteView.frame = CGRectMake(0, 0, size.width, self.topWhiteView.frame.size.height);
    } completion:nil];
}
- (BOOL)shouldAutorotate
{
    return [PYScreenRotateManager shouldAutorotate];
}

- (UIInterfaceOrientationMask)supportedInterfaceOrientations
{
//    if ([PYScreenRotateManager supportedInterfaceOrientations] == UIInterfaceOrientationMaskPortrait) {
//        [UIApplication sharedApplication].statusBarOrientation = [PYScreenRotateManager statusBarOrientation];
//    }
    return [PYScreenRotateManager supportedInterfaceOrientations];
}
/// 实现这个方法，解决导航栏问题,present 
//- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
//{
//    return [PYScreenRotateManager statusBarOrientation];
//}

@end
